package cn.magic.basic.controller;

import cn.magic.basic.dto.SmsCodeDto;
import cn.magic.basic.exception.BusinessException;
import cn.magic.basic.service.IVerifyCodeService;
import cn.magic.basic.utils.AjaxResult;
import cn.magic.org.domain.ShopAuditLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
* @Description: 公共图形验证码controller层
* @Author: Mr.pc
* @Version: 1.0
* @Date:  2022/7/4 16:52
*/
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
    * @Title: graph
    * @Description: 获取图形验证码
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/4 16:57
    * @Parameters: []
    * @Return cn.magic.basic.utils.AjaxResult
    */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey") String imageCodeKey){
        try {
            String imageBase64Str = verifyCodeService.getImageCode(imageCodeKey);
            return AjaxResult.me().setResultObj(imageBase64Str);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！");
        }
    }


    /**
    * @Title: smsCode
    * @Description: 手机发送验证码
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/6 12:59
    * @Parameters: [smsCodeDto]
    * @Return cn.magic.basic.utils.AjaxResult
    */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
           verifyCodeService.smsCode(smsCodeDto);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！");
        }
    }





}
